package com.samp.solr.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

import com.google.gson.Gson;

public class SolrClientUtils {
	
	 private final static String SOLR_URL = "http://localhost:8080/solr/demo";
	 
	 private static final String BASE_PATH = "D:\\temp\\jsonToSolr\\";
	 
	 private static HttpSolrClient solrClient;
	 
	 
	 
	 private static HttpSolrClient getSolrClient(){
		 if( solrClient == null ){
			 solrClient = new HttpSolrClient.Builder(SOLR_URL).build();
		 }
		 return solrClient;
	 }
	 
	 
	private static  void addDirFilesToSolr() throws Exception{
		 HttpSolrClient solrClient = getSolrClient();
		 Path dir = Paths.get(BASE_PATH);
		 Gson gson = new Gson();
		 try( DirectoryStream<Path> stream = Files.newDirectoryStream(dir) ){
	            for(Path path : stream){
	            	if( !Files.isDirectory(path) ){
	            		String jsonStr = getContentFromFile(path);
	            		SolrDataDTO solrData = gson.fromJson(jsonStr, SolrDataDTO.class);
	            		solrData.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
	            		solrClient.addBean(solrData);
	            	}
	            }
	            solrClient.commit();
	        }catch(IOException e){
	            e.printStackTrace();
	        }finally{
	        	solrClient.close();
	        }
	 }
	 
	 private static String getContentFromFile(Path filePath) throws IOException{
		 	BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
		 	StringBuilder sb = new StringBuilder();
			try {
				String line = null;
				while( (line = bufferReader.readLine()) != null ){
					sb.append(line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				bufferReader.close();
			}
			return sb.toString();
	 }
	 
	 @SuppressWarnings("unused")
	private static void generateTestData() throws Exception {
		 	DocDTO doc1 = new DocDTO("第一章","http://001","同义词高亮问题");
			DocDTO doc2 = new DocDTO("第二章","http://001","建索引前先删除数据");
			DocDTO doc3 = new DocDTO("第三章","http://001","图像建索引，同一ID看能否删除");
			DocDTO doc4 = new DocDTO("第四章","http://001","图像搜索diff最低值");
			List<DocDTO> docList = new ArrayList<DocDTO>();
			docList.add(doc1);
			docList.add(doc2);
			docList.add(doc3);
			docList.add(doc4);
			Gson gson = new Gson();
			for( DocDTO doc : docList ){
				String jsonStr = gson.toJson(doc);
				Path path = Paths.get(BASE_PATH, doc.getTitle()+".txt");
				if( !Files.exists(path) ){
					Files.createFile(path);
				}
				Files.write(path, jsonStr.getBytes(), StandardOpenOption.APPEND);
			}
	 }
	 

	public static void main(String[] args)  {
		try {
			//generateTestData();
			addDirFilesToSolr();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

class DocDTO{
	private String title;
	private String url;
	private String content;
	
	public DocDTO(String title, String url , String content){
		this.title = title;
		this.url = url;
		this.content = content;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
}
